import pandas as pd
import numpy as np


def index_calculate(output: pd.DataFrame):
    cost = output['stat_cost']
    click = output['click_cnt']
    show = output['show_cnt']
    convert = output['convert_cnt']
    cvr = (convert/click).fillna(0)
    cvr[cvr == np.inf] = 0
    ctr = (click/show).fillna(0)
    ctr[ctr == np.inf] = 0
    cpm = (1000*cost/show)
    cpa = (cost/convert)
    acp = (cost/click)
    epsilon = 0.1
    cpm = np.exp(-epsilon*cpm).fillna(0)
    cpa = np.exp(-epsilon*cpa).fillna(0)
    acp = np.exp(-epsilon*acp).fillna(0)
    alpha = 2
    index_cost = 1 - np.exp(-alpha*cost).fillna(0)
    index = pd.concat([cvr, ctr, cpm, cpa, acp, index_cost], axis=1)
    # index.columns = ['cvr', 'ctr', 'cpm', 'cpa', 'acp']
    # index.to_csv(r'.//index.csv')
    weight = np.array([[1/6, 1/6, 1/6, 1/6, 1/6, 1/6]])
    weight = weight.T
    AEI = np.dot(index, weight)
    AEI = pd.DataFrame(AEI)
    # advertise effect index
    # AEI.to_csv(r'.//AEI.csv')
    return AEI


data = pd.read_csv(r'.//output.csv')
AEI = index_calculate(data)
AEI.to_csv(r'.//AEI.csv')
